package com.hollycrm.unicom.sheet.business.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.hollycrm.unicom.security.AuthToken;
import com.hollycrm.unicom.sheet.BeanResource;
import com.hollycrm.unicom.sheet.Constants;
import com.hollycrm.unicom.sheet.business.BusinessSupportManager;
import com.hollycrm.unicom.sheet.domain.SheetRoutineNote;
import com.hollycrm.unicom.sheet.flowsupport.DoOperateContext;

/**
 * SheetRoutineNoteAction
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author Tian Kai
 * @version 1.0
 */
public class SheetRoutineNoteAction
    extends AbstractCommitAction {
    /**
     * execDefault
     *
     * @param mapping ActionMapping
     * @param actionForm ActionForm
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @throws Exception
     * @return ActionForward
     */
    public ActionForward execDefault(ActionMapping mapping,
                                     ActionForm actionForm,
                                     HttpServletRequest request,
                                     HttpServletResponse response) throws
        Exception {
        String sheetFlowNO = request.getParameter("sheetFlowNO");
        BusinessSupportManager bsm = getBusinessSupportManager((SheetRoutineNoteForm)actionForm);
        List routineNotes = bsm.listSheetRoutineNote(sheetFlowNO);
        request.setAttribute("routineNotes", routineNotes);
        return mapping.findForward("list");
    }

    /**
     * execView
     *
     * @param mapping ActionMapping
     * @param actionForm ActionForm
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @throws Exception
     * @return ActionForward
     */
    public ActionForward execView(ActionMapping mapping,
                                     ActionForm actionForm,
                                     HttpServletRequest request,
                                     HttpServletResponse response) throws
        Exception {
        String sheetFlowNO = request.getParameter("sheetFlowNO");
        BusinessSupportManager bsm = getBusinessSupportManager((SheetRoutineNoteForm)actionForm);
        List routineNotes = bsm.listSheetRoutineNote(sheetFlowNO);
        request.setAttribute("routineNotes", routineNotes);
        return mapping.findForward("poplist");
    }


    /**
     * execAddForm
     *
     * @param mapping ActionMapping
     * @param actionForm ActionForm
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @throws Exception
     * @return ActionForward
     */
    public ActionForward execAddForm(ActionMapping mapping,
                                     ActionForm actionForm,
                                     HttpServletRequest request,
                                     HttpServletResponse response) throws
        Exception {
        SheetRoutineNoteForm form = (SheetRoutineNoteForm) actionForm;
        AuthToken authToken = (AuthToken) request.getSession().getAttribute(
            AuthToken.BEAN_ID);

        SimpleDateFormat sdf = (SimpleDateFormat) getBean(BeanResource.SDF_Y2S);
        ( (SheetRoutineNote) form.getRoutineNote()).setAgentCode(authToken.
            getUserCode());
        ( (SheetRoutineNote) form.getRoutineNote()).setBeginTime(sdf.format(new
            Date()));

        saveToken(request);

        return mapping.findForward("addForm");
    }

    /**
     * setupCommitContext
     *
     * @param actionForm ActionForm
     * @param request HttpServletRequest
     * @param doc DoOperateContext
     * @return String
     */
    public String setupCommitContext(ActionForm actionForm,
                                     HttpServletRequest request,
                                     DoOperateContext doc) {
        SheetRoutineNoteForm form = (SheetRoutineNoteForm) actionForm;
        SheetRoutineNote routineNote = (SheetRoutineNote) form.getRoutineNote();
        routineNote.setEndTime( ( (SimpleDateFormat) getBean(
            BeanResource.SDF_Y2S)).format(new Date()));
        doc.addInputAttr(routineNote);
        return null;
    }

    /**
     * getBusinessSupportManager
     *
     * @param form MainSheetForm
     * @return BusinessSupportManager
     */
    private BusinessSupportManager getBusinessSupportManager(SheetRoutineNoteForm form) {
        if (Constants.TRUE.equals(form.getArchive()))
            return (BusinessSupportManager) getBean(BeanResource.
                ARCHIVEBUSINESSSUPPORT_MANAGER);
        else
            return (BusinessSupportManager) getBean(BeanResource.
                BUSINESSSUPPORT_MANAGER);
    }

}
